<link rel="stylesheet" href="/plug/layui/css/layui.css">
<div style="margin: 40px;">
    <form class="layui-form" xmlns:th="http://www.thymeleaf.org">
        <input type="hidden" name="id" th:value="${gradeClass == null ? '' : gradeClass.id}">
        <input type="hidden" name="parentId" value="0">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">(年级/班级)名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" maxlength="100" th:value="${gradeClass == null ? '' : gradeClass.name}" placeholder="请输入(年级/班级)名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年级/班级</label>
            <div class="layui-input-block">
                <input type="radio" name="deep" lay-filter="deep" value="1" title="年级" checked>
                <input type="radio" name="deep" lay-filter="deep" value="2" title="班级">
            </div>
        </div>
        <div class="layui-form-item layui-form-select layui-hide" id="grade">
            <label class="layui-form-label">年级</label>
            <div class="layui-input-block">
                <select name="grade" lay-filter="grade">
                    <option th:each="data: ${gradeList}" th:value="${data.id}" th:text="${data.name}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" lay-verify="required" th:value="${gradeClass == null ? '' : gradeClass.sort}" placeholder="请输入排序" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/plug/layui/layui.all.js"></script>
<script>
    //加载layui
    layui.use(['layer', 'form','laydate'], function(){
        var layer = layui.layer
            ,form = layui.form;

        //layui提交
        form.on('submit(submit)', function(data){
            $.ajax({
                data:JSON.parse(JSON.stringify(data.field)),
                type:"post",
                url:"/gradeClass/edit",
                success:function (rest) {
                    layer.msg(rest.msg);
                    if(rest.code === 200) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function () {
                    layer.msg("发生错误，请联系管理员");
                }
            });
            return false;
        });

        form.on('radio(deep)', function(data){
            if(data.value === '1') {
                $("input[name=parentId]").val(0);
                $('#grade').addClass("layui-hide");
            }else {
                $('#grade').removeClass("layui-hide");
                $("input[name=parentId]").val($('select[name=grade]').val());
            }
        });

        form.on('select(grade)', function (data) {
            $("input[name=parentId]").val(data.value);
        })
    });
</script>